<template>
  <div class="app-container">
    <el-tabs v-model="queryParams.state" @tab-click="handleQuery">
      <el-tab-pane
        :label="dict.label"
        :name="dict.value"
        v-for="(dict, index) in dict.type.approval_status"
        :key="index"
      ></el-tab-pane>
    </el-tabs>
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item :label="$t('approval_type')" prop="approvalType">
        <el-select
          v-model="queryParams.approvalType"
          :placeholder="$t('please_select', { name: $t('approval_type') })"
          clearable
        >
          <el-option
            v-for="dict in dict.type.approval_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >{{ $t("search") }}</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{
          $t("reset")
        }}</el-button>
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="formList"
      @selection-change="handleSelectionChange"
      border
      :max-height="tableHeight"
    >
      <el-table-column
        :label="$t(approval_type)"
        align="center"
        prop="eventType"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.approval_type"
            :value="scope.row.eventType"
          />
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('initiator')"
        align="center"
        prop="empName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        :label="$t('employing_company')"
        align="center"
        prop="employerName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        :label="$t('initiation_time')"
        align="center"
        prop="createTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('summary')"
        align="center"
        prop="remark"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        :label="$t('current_node')"
        align="center"
        prop="formAbstract"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        :label="$t('action')"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            >{{ $t("view") }}</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
      :page-sizes="[10,20,30,50,100, 200]"
    />

    <!-- 添加或修改审批对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="1000px"
      append-to-body
    >
      <el-form style="padding: 0 20px" label-suffix="：">
        <el-row type="flex" style="border: 1px solid #f0f2f5">
          <el-col :span="12">
            <div class="box-block">
              <el-form-item :label="$t('applicant')">{{
                form.empName
              }}</el-form-item>
              <el-form-item :label="$t('approval_type')">
                <dict-tag
                  :options="dict.type.approval_type"
                  :value="form.eventType"
                />
              </el-form-item>
              <template v-if="form.eventType == 1">
                <el-form-item :label="$t('modify_check_date')">
                  {{ form.workday }}
                </el-form-item>
                <el-form-item :label="$t('modify_clock_type')">
                  {{
                    form.punchType == 1
                      ? $t("work_card")
                      : form.punchType == 2
                      ? $t("off_duty_card")
                      : "-"
                  }}
                </el-form-item>
                <el-form-item :label="$t('original_check_time')">
                  {{ form.punchInTime }}
                </el-form-item>
                <el-form-item :label="$t('modified_time')">
                  {{ form.changeTime }}
                </el-form-item>
              </template>
              <template v-if="form.eventType == 2">
                <el-form-item :label="$t('application_time')">
                  {{ form.workday }}
                </el-form-item>
                <el-form-item :label="$t('overtime_hours')">
                  {{ form.duration }}
                </el-form-item>
              </template>
              <template v-if="form.eventType == 3">
                <el-form-item :label="$t('holiday_type')">
                  {{ form.typeName }}
                </el-form-item>
                <el-form-item :label="$t('leave_period')">
                  {{ form.dayStart }} - {{ form.dayEnd }}
                </el-form-item>
                <el-form-item :label="$t('reason_taking_leave')">
                  {{ form.content }}
                </el-form-item>
              </template>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="box-block">
              <el-steps direction="vertical" :space="70" :active="1">
                <el-step v-for="(item, index) in flowList" :key="index">
                  <div class="stepContent" slot="title">
                    <div class="stepLeft">
                      <div class="stepTitle">
                        {{ item.title }}
                        <span v-if="index == 0"
                          >({{ item.approveName }}{{ $t("initiate") }})</span
                        >
                        <span v-else
                          >({{ item.approveName
                          }}{{
                            item.status == 1
                              ? $t("agree")
                              : item.status == 2
                              ? $t("reject")
                              : $t("in_review")
                          }})</span
                        >
                      </div>
                      <div class="stepDescription" v-if="item.status">
                        {{ item.status == 1 ? $t("agree") : $t("reject") }}
                      </div>
                    </div>
                    <div class="stepRight" v-if="item.createTime">
                      {{ item.createTime }}
                    </div>
                  </div>
                </el-step>
              </el-steps>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <!-- <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">拒 绝</el-button>
        <el-button type="primary" @click="submitForm">同 意</el-button>
      </div> -->
    </el-dialog>
  </div>
</template>

<script>
import { listForm, getForm } from "@/api/custom/approve";

export default {
  name: "Approve",
  dicts: [
    "sys_common_status",
    "approval_type",
    "leave_type",
    "approval_status",
  ],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 审批表格数据
      formList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        approvalType: null,
        state: "1",
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      lookView: true,
      flowList: [],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询审批列表 */
    getList() {
      this.loading = true;
      listForm({
        ...this.queryParams,
      }).then((response) => {
        this.formList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.$prompt(
        this.$t("please_enter", { name: this.$t("reason_refusal") }),
        this.$t("reason_refusal"),
        {
          confirmButtonText: this.$t("confirm"),
          cancelButtonText: this.$t("cancel"),
          inputType: "textarea",
          inputPattern: /^.+$/,
          inputErrorMessage: this.$t("cannot_empty", {
            name: this.$t("reason_refusal"),
          }),
        }
      )
        .then(({ value }) => {
          console.log("拒绝理由: " + value);
          this.open = false;
          // updateEmployee(this.form).then((response) => {
          //   this.$modal.msgSuccess("修改成功");
          //   this.open = false;
          //   this.$emit("refreshList");
          // });
        })
        .catch(() => {});
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 查看按钮操作 */
    handleView(row) {
      this.form = { ...row };
      getForm({
        incident: row.eventType == 1 ? 3 : row.eventType == 2 ? 4 : 0,
        employerId: row.employerId,
        empId: row.empId,
      }).then((response) => {
        const list = response.data.map((item, index) => {
          item.title = `${index + 1}${$t("level_approval")}`;
          return item;
        });
        list.unshift({
          title: this.$t("initiate_application"),
          approveName: row.empName,
          status: null,
          createTime: row.createTime,
        });
        this.flowList = list;
        this.open = true;
        this.title = this.$t("view");
        this.lookView = true;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.open = false;
      return;
      updateForm(this.form).then((response) => {
        this.$modal.msgSuccess("修改成功");
        this.open = false;
        this.getList();
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm(this.$t("delete_data"))
        .then(function () {
          return delForm(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess(this.$t("operation_successful"));
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/form/export",
        {
          ...this.queryParams,
        },
        `form_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style lang="scss" scoped>
.box-block {
  padding: 8px 16px;
  border-left: 1px solid #f0f2f5;
  border-right: 1px solid #f0f2f5;
  border-radius: 0px;
  .stepContent {
    display: flex;
    align-items: center;
    justify-content: space-between;
    .stepLeft {
      flex: 1;
      margin-right: 20px;
      .stepTitle {
        span {
          color: #999;
          font-size: 12px;
          margin-left: 10px;
        }
      }
      .stepDescription {
        color: #c0c4cc;
        font-size: 12px;
      }
    }
    .stepRight {
      color: #c0c4cc;
      font-size: 12px;
    }
  }
}
</style>
